String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g,"");};

function updatePage(xmlHttp, divId,flag) {
	return function() {
		if (xmlHttp.readyState == 4) {
			var response = xmlHttp.responseText;
                         if(divId != null && divId != "" &&document.getElementById(divId) != null)
			document.getElementById(divId).innerHTML = response;
			xmlHttp.abort();
			xmlHttp = null;
			if(flag == 1)loadProject();
			else if( flag == 2)loadMember(0);
			else if( flag == 3){
				loadProject();
				loadMember(0);
			}else if(flag == 4){
                                loadManage();
                        }else if(flag == 6){
                                loadMember(0);
                                loadMenu();
                        }
		}
	};
};

function loadPage(url, divId,flag) {
	var xmlHttp = false;
	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			xmlHttp = false;
		}
	}
	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}
        var now = new Date();
        if(url.indexOf('?') > 0) url = url + "&&";
        else url = url + '?';
        url = url + "extra=" + now.getYear()+"_"+now.getMonth()+"_"+now.getDate()+"_"+now.getHours()+"_"+now.getMinutes()+"_"+now.getSeconds() + "_" + now.getMilliseconds();
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = updatePage(xmlHttp, divId,flag);
	xmlHttp.send(null);
};

function loadProject() {
	loadPage("project.jsp", "project");
};

function loadMember(pid) {
	loadPage("member.jsp?pid=" + pid, "book",1);
};

function loadAdvice() {
	loadPage("advice.jsp", "advice");
};

function loadManage(){
	loadPage("manage.jsp", "manage");
};
function modifyMenu(){
        var content = document.getElementById("menuContent").value;
        var url = "menuManage.jsp?menuContent="+content;
        url = encodeURI(encodeURI(url));
        loadPage(url,"manageStatus",6);
}

function bookDinner(target,mid) {
	if(target.checked){
		document.getElementById("mid"+mid).innerHTML = "订购中...";
		loadPage("dinner.jsp?action=book&&mid="+mid,"mid"+mid,1);
	}else{
		document.getElementById("mid"+mid).innerHTML = "取消中...";
		loadPage("dinner.jsp?action=cancel&&mid="+mid,"mid"+mid,1);
	}
};

function authorize(page){
	var mname = document.getElementById("manageName").value;
	var mpass = document.getElementById("managePass").value;
	loadPage(page+".jsp?mname="+mname+"&&mpass="+mpass,page,3);
};

function exportBook(){
        var beginDate = document.getElementById("beginDate").value.trim();
        var endDate = document.getElementById("endDate").value.trim();
        var regdate = /^[0-9]{4}(-([0-9]){2}){2}$/;
        if(regdate.test(beginDate)  && regdate.test(endDate)){
            var subwin=window.open("export.jsp?beginDate="+beginDate+"&&endDate="+endDate);
        }else{
            alert('时间格式不正确！');
        }
};

function addMember(){
	var pid = document.getElementById("manageProject").value;
	var mname = document.getElementById("manageMember").value;
	if(mname != null && mname != ""){
             var url = "addMember.jsp?pid="+pid + "&&mname="+mname;
             url = encodeURI(encodeURI(url));
             loadPage(url,"manageStatus",3);
	}
};

function linkManage(){
    loadManage();
};

function closeManage(){
    document.getElementById("manage").innerHTML = "";
    loadPage("logout.jsp","",2);
};

function deleteMember(mid){
    var isreal = confirm("确定要删除么？");
    if(isreal){
         loadPage("deleteMember.jsp?mid="+mid,"tips",3);
    }
};
function lockDinner(action){
    loadPage("lockDinner.jsp?action="+action,"tips",4);
}
function loadSystem() {
    loadProject();
    loadMember(0);
    loadAdvice();
    loadMenu();
};
function loadMenu(){
    loadPage("showMenu.jsp","menu");
}
function joinDinner(){
    loadPage("joinDinner.jsp","register",5);    
}
function joinNow(){
    var pid = document.getElementById("joinProject").value;
    var did = document.getElementById("joinDepartment").value;
    var mname = document.getElementById("joinName").value;
    var fid = document.getElementById("joinfid").value;
    if(pid == null || pid.trim() == "") return ;
    if(mname == null || mname.trim() == "") return ;
    var url = "joinMe.jsp?pid="+pid + "&&did=" + did + "&&fid=" + fid + "&&mname="+mname;
    url = encodeURI(encodeURI(url));
    loadPage(url,"joinStatus",3);
}
function closeJoin(){
    document.getElementById("register").innerHTML = "<span></span>";
}

function addProject(){
    var pname = document.getElementById("proName").value;
    if(pname !=null) pname = pname.trim();
    if(pname == null || pname == "") return;
    var url = "addProject.jsp?pname="+pname;
    url = encodeURI(encodeURI(url));
    loadPage(url,"manageStatus",1);
}
function showOperator(actionIp){
    hideOperator();
    var mouseX = event.clientX + document.body.scrollLeft;
    var mouseY = event.clientY + document.body.scrollTop;
    var oper = document.createElement("div");
    oper.setAttribute("id","operator");
    oper.style.left = "" +(mouseX + 10) + "px";
    oper.style.top = "" + (mouseY + 5) + "px";
    document.getElementById("root").appendChild(oper);
    loadPage("showOperator.jsp?actionIp=" + actionIp,"operator");
}
function hideOperator(){
    if(document.getElementById("operator") != null){
        document.getElementById("root").removeChild(document.getElementById("operator"));
    }
}

function changeFloor(target,mid){
    var fid = target.value;
    var url = "changeFloor.jsp?mid="+mid+"&&fid="+fid;
    loadPage(url,"tips");
}

function addFloor(){
    var fname = document.getElementById("floorName").value;
    if(fname != null)fname = fname.trim();
    if(fname == null || fname == "") return;
    var regname =  /^[0-9]{1,2}层$/;
    if(regname.test(fname)){
        var url = "addFloor.jsp?fname="+fname;
        url = encodeURI(encodeURI(url));
        loadPage(url,"tips",2);
    }else{
        alert("楼层填错啦，填写格式如'21层'");
    }
}


